<template>
	<div>
		<!--引用表格-->
		<BasicTable @register="registerTable" :rowSelection="rowSelection">
			<!--插槽:table标题-->
			<template #tableTitle>
				<a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增</a-button>
				<a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</a-button>
				<j-upload-button type="primary" preIcon="ant-design:import-outlined"
					@click="onImportXls">导入</j-upload-button>
				<a-dropdown v-if="selectedRowKeys.length > 0">
					<template #overlay>
						<a-menu>
							<a-menu-item key="1" @click="batchHandleDelete">
								<Icon icon="ant-design:delete-outlined"></Icon>
								删除
							</a-menu-item>
						</a-menu>
					</template>
					<a-button>批量操作
						<Icon icon="mdi:chevron-down"></Icon>
					</a-button>
				</a-dropdown>
			</template>
			<!--操作栏-->
			<template #action="{ record }">
				<TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
			</template>
			<!--字段回显插槽-->
			<template v-slot:bodyCell="{ column, record, index, text }">
			</template>
		</BasicTable>
		<!-- 表单区域 -->
		<BsTravelgroupMaintuortravellinerecordModal @register="registerModal" @success="handleSuccess">
		</BsTravelgroupMaintuortravellinerecordModal>
		<TenantRecycleBinModal @register="registerIndexModal" @successConfirm="handleSuccessIndex">
		</TenantRecycleBinModal>
		<uploadImgModal @register="registerUploadImgModal" @successConfirm="uploadImgModalFun" />
		<EditMainDrawer @register="registerDrawer" @success="handleSuccess">
		</EditMainDrawer>
		<uploadStatsModal @register="registerUploadStatsModal" @successConfirm="uploadImgModalFun" />
		<QrImgModal @register="registerQrImgModal" @success="handleSuccess">
				</QrImgModal>
	</div>
</template>

<script lang="ts" name="journey-bsTravelgroupMaintuortravellinerecord" setup>
	import { ref, computed, unref } from 'vue';
	import { BasicTable, useTable, TableAction } from '/@/components/Table';
	import { useModal } from '/@/components/Modal';
	import { useListPage } from '/@/hooks/system/useListPage'
	import BsTravelgroupMaintuortravellinerecordModal from './components/BsTravelgroupMaintuortravellinerecordLittleModal.vue'
	import { columns, searchFormSchema } from './BsTravelgroupMaintuortravellinerecordLittle.data';
	import { list, deleteOne, batchDelete, getImportUrl, getExportUrl, getMsgsByGroup, getMsgByPreline } from './BsTravelgroupMaintuortravellinerecord.api';
	//import { downloadFile } from '/@/utils/common/renderUtils';
	import TenantRecycleBinModal from '../../../module/toHomePageModal.vue';
	import { useUserStore } from '/@/store/modules/user';
	import { formatTimestamp } from '/@/utils/common/renderUtils';
	const checkedKeys = ref<Array<string | number>>([]);
	import EditMainDrawer from './drawer/index.vue'
	import QrImgModal from '../../../module/qrCodeImg.vue'
	import uploadImgModal from '../bigteam/apply/modal/uploadQrImgModal.vue';
	import uploadStatsModal from './components/updateStatsModal.vue'
	import { useDrawer } from '/@/components/Drawer';
	const userStore = useUserStore();
	const [registerDrawer, { openDrawer, closeDrawer }] = useDrawer();
	//注册model
	const [registerModal, { openModal }] = useModal();
	const [registerIndexModal, { openModal: openIndexModal }] = useModal();
	const [registerUploadStatsModal, { openModal: openUpdateStatsModal }] = useModal();
	const [registerQrImgModal, { openModal: openQrImgModal }] = useModal();

	//注册table数据
	const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
		tableProps: {
			title: '拼车行程',
			api: list,
			columns,
			canResize: false,
			formConfig: {
				//labelWidth: 120,
				schemas: searchFormSchema,
				autoSubmitOnEnter: true,
				showAdvancedButton: true,
				fieldMapToNumber: [
				],
				fieldMapToTime: [
				],
			},
			beforeFetch(params) {
				params.type = 'Self'
				return params
			},
			actionColumn: {
				width: 200,
				fixed: 'left'
			},
		},
		exportConfig: {
			name: "客户旅游路线主记录",
			url: getExportUrl,
		},
		importConfig: {
			url: getImportUrl,
			success: handleSuccess
		},
	})

	const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext

	/**
	 * 新增事件
	 */
	async function handleAdd() {

		let o = await getMsgsByGroup();
		let record = {
			retrofitRule: o.retrofitRule,
			mustSee: o.mustSee
		}
		openDrawer(true, {
			record,
			isUpdate: false,
			showFooter: true,
			isCopy:false
		});
		/* 	openModal(true, {
				isUpdate: false,
				showFooter: true,
			}); */
	}
	/**
	 * 编辑事件
	 */
	function handleEdit(record : Recordable) {


		record.startdateStr = formatTimestamp(record.startdate)
		record.expiredateStr = formatTimestamp(record.expiredate)
		let extInfor = JSON.parse(record.extInfor)
		record.groupUserSize = extInfor.groupUserSize
		record.virtualUserNum = extInfor.virtualUserNum
		record.cityId = record.startFromCityId
		record.destination = extInfor.destination
		record.msg = extInfor.msg
		record.keyCode = extInfor.keyCode
		record.startdateSj = extInfor.startdateStr
		record.enddateStr = extInfor.enddateStr
		record.descriptionOfFees = extInfor.descriptionOfFees
		record.retrofitRule = extInfor.retrofitRule
		record.tags = extInfor.tags
		record.mustSee = extInfor.mustSee
		record.suggestedTime = extInfor.suggestedTime
		record.xjSelect = extInfor.xjSelect
		record.pdtIds = extInfor.pdtIds
		record.sjSelect = extInfor.sjSelect

		//return
		if (record.fqrImages) {

			record.fqrImages = JSON.parse(record.fqrImages).join(',')
			console.log(record.fqrImages)
		}

		//这里获取出来id

		console.log(record.pretravelcarlist)
		if (record.pretravelcarlist.indexOf('[') < 0) {
			record.pretravelcarlist1 = record.pretravelcarlist.split(',')
		}else{
			let pl = JSON.parse(record.pretravelcarlist)
			
			record.pretravelcarlist1 = pl.join(',')
		}

		openDrawer(true, {
			record,
			isUpdate: true,
			showFooter: true,
			isCopy:false
		});

		/* 	openModal(true, {
				record,
				isUpdate: true,
				showFooter: true,
			}); */
	}
	
	function handleCopy(record : Recordable) {
	
	
		record.startdateStr = formatTimestamp(record.startdate)
		record.expiredateStr = formatTimestamp(record.expiredate)
		let extInfor = JSON.parse(record.extInfor)
		record.groupUserSize = extInfor.groupUserSize
		record.virtualUserNum = extInfor.virtualUserNum
		record.cityId = record.startFromCityId
		record.destination = extInfor.destination
		record.msg = extInfor.msg
		record.keyCode = extInfor.keyCode
		record.startdateSj = extInfor.startdateStr
		record.enddateStr = extInfor.enddateStr
		record.descriptionOfFees = extInfor.descriptionOfFees
		record.retrofitRule = extInfor.retrofitRule
		record.tags = extInfor.tags
		record.mustSee = extInfor.mustSee
		record.suggestedTime = extInfor.suggestedTime
		record.xjSelect = extInfor.xjSelect
		record.pdtIds = extInfor.pdtIds
		record.sjSelect = extInfor.sjSelect
	
		//return
		if (record.fqrImages) {
	
			record.fqrImages = JSON.parse(record.fqrImages).join(',')
			console.log(record.fqrImages)
		}
	
		//这里获取出来id
	
		console.log(record.pretravelcarlist)
		if (record.pretravelcarlist.indexOf('[') < 0) {
			record.pretravelcarlist1 = record.pretravelcarlist.split(',')
		}else{
			let pl = JSON.parse(record.pretravelcarlist)
			
			record.pretravelcarlist1 = pl.join(',')
		}
	
		openDrawer(true, {
			record,
			isUpdate: true,
			showFooter: true,
			isCopy:true
		});
	
		/* 	openModal(true, {
				record,
				isUpdate: true,
				showFooter: true,
			}); */
	}
	
	function indexEdit(record : Rcordable) {
		let o = JSON.parse(record.extInfor)

		openIndexModal(true, {
			typeIds: o.typeIds,
			typeTitles: o.typeTitles,
			mainId: record.id,
			isUpdate: false,
			sort: o.sort,
			showFooter: true,
		})
	}
	function handleSuccessIndex() {
		(selectedRowKeys.value = []) && reload();

	}


	/**
	 * 详情
	*/
	function handleDetail(record : Recordable) {
		record.startdateStr = formatTimestamp(record.startdate)
		record.expiredateStr = formatTimestamp(record.expiredate)
		openModal(true, {
			record,
			isUpdate: true,
			showFooter: false,
		});
	}
	/**
	 * 删除事件
	 */
	async function handleDelete(record) {
		await deleteOne({ id: record.id }, handleSuccess);
	}
	/**
	 * 批量删除事件
	 */
	async function batchHandleDelete() {
		await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
	}
	/**
	 * 成功回调
	 */
	function handleSuccess() {
		(selectedRowKeys.value = []) && reload();
	}
	
	
	function handleShare(record : Recordable) {
		//生成小程序二维码和分享链接
		openQrImgModal(true, {
			isUpdate: true,
			record,
			showFooter: false
		})
	
	}
	/**
	   * 操作栏
	   */
	function getTableAction(record) {
		return [
			{
				label: '编辑',
				onClick: handleEdit.bind(null, record),
			},
			{
				label: '推广',
				onClick: handleShare.bind(null, record),
			},
			{
				label: '复制',
				onClick: handleCopy.bind(null, record),
			}
		]
	}
	const [registerUploadImgModal, { openModal: openUploadQrImg }] = useModal();


	function openUploadQrImgModal(e) {
		openUploadQrImg(true, {
			e,
			isUpdate: false,
			showFooter: true
		})
	}
	function uploadImgModalFun() {
		reload();
	}

	function handleUpdateStats(record) {
		let data = {
			isOrder: 0,
			id: record.id,
			stats: record.stats
		}
		openUpdateStatsModal(true, {
			data,
			isOrder: 0,
			isUpdate: true,
			showFooter: true
		})
	}

	/**
	   * 下拉操作栏
	   */
	function getDropDownAction(record) {
		return [
			{
				label: '修改状态',
				onClick: handleUpdateStats.bind(null, record),
			},
			{
				label: '上传二维码',
				onClick: openUploadQrImgModal.bind(null, record),
			},
			{
				label: '推荐到首页',
				onClick: indexEdit.bind(null, record),
			},
			{
				label: '删除该行程',
				popConfirm: {
					title: '是否确认删除',
					confirm: handleDelete.bind(null, record),
					placement: 'topLeft',
				}
			}
		]
	}
</script>

<style scoped>

</style>